package com.starlight.easybuyprictice.service;

import com.starlight.easybuyprictice.mapper.MsMemberMapper;
import com.starlight.easybuyprictice.entity.MsMember;
import com.starlight.easybuyprictice.entity.MsMemberExample;
import com.starlight.easybuyprictice.entity.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;

/**
 * @Author yfy2001
 * @date 2022/11/28 21:39
 */
@Service
public class MemberManageService {
    @Autowired
    private final MsMemberMapper msMemberMapper;
    @Autowired
    private final TokenManager tokenManager;

    public MemberManageService(MsMemberMapper msMemberMapper, TokenManager tokenManager) {
        this.msMemberMapper = msMemberMapper;
        this.tokenManager = tokenManager;
    }


    public Result login(String uname,String password){
        Result result = new Result();
        List<MsMember> members = findMemberByUname (uname);
        if (members.size()!= 1) {
            result.setMsg("请先注册会员");
            return result;
        }

        MsMember foundMember = members.get(0);
        if (foundMember.getPassword().equals(password)) {
            if ( ! updateLoginTimestamp(foundMember.getMemberId())){
                result.setMsg("更新最近一次登录时间失败");
                result.setStatus(3);
                return result;
            }

            Token token = tokenManager.createToken(foundMember.getMemberId(),
                    Token.Type.values()[foundMember.getRole()]);
            result.setData(token.getValue());
            result.setStatus(0);
        }else{
            result.setStatus(2);
            result.setMsg("密码错误");
        }
        return result;
    }

    public Result register(String uname,String password) {
        Result result = new Result();
        List<MsMember> members = findMemberByUname(uname);
        if (members.size() == 1) {
            result.setStatus(1);
            result.setMsg("用户名已存在，请直接登录或者更换用户名");
            return result;
        }
        if (addMember(uname, password)) {
            result.setStatus(0);
            result.setMsg("注册成功");
        } else {
            result.setStatus(2);
            result.setMsg("注册失败");
        }
        return result;
    }
    public Result logout(String tokenValue) {
        tokenManager.deleteToken(new Token(tokenValue));
        return new Result();
    }
    private boolean addMember(String uname,String password) {
        MsMember member = new MsMember();
        member.setUname(uname);
        member.setPassword(password);
        return msMemberMapper.insertSelective(member) == 1;
    }
    private List<MsMember> findMemberByUname(String uname) {
        MsMemberExample example = new MsMemberExample();
        MsMemberExample.Criteria criteria = example.createCriteria();
        criteria.andUnameEqualTo(uname);
        return msMemberMapper.selectByExample(example);
    }
    private boolean updateLoginTimestamp(int memberId){
        MsMember msMember = new MsMember();
        msMember.setMemberId(memberId);
        msMember.setLastlogin(new Date());
        return msMemberMapper.updateByPrimaryKeySelective(msMember) == 1;
    }

    public Result listMember(){
        Result result=new Result();
        List<MsMember> msMemberList = msMemberMapper.selectAllMember();
        result.setData(msMemberList);
        return result;
    }

}
